var items = new Array();
$(function() {
	$('#addForm').submit(function() {
		$.ajax({
			url: '/wms/cmsmd/saveCmsmd.xhtml',
			data: $('#addForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("增加成功!");
					closeDialogAdd();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$('#updateForm').submit(function() {
		$.ajax({
			url: '/wms/cmsmd/updateCmsmd.xhtml',
			data: $('#updateForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("修改成功!");
					closeDialogUpdate();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$('#pdaForm').submit(function() {
		$.ajax({
			url: '/wms/cmsml/pdaRole.xhtml',
			data: $('#pdaForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("修改成功!");
					closeDialogPda();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$("#checkallupdate").click(function() {
		if($(this).attr("checked") == "checked") {
			$("[name='daaaUpdate']").attr("checked", 'true');
		} else {
			$("[name='daaaUpdate']").removeAttr("checked");
		}
	});
	ajaxTable();
	setDialogSearch();
	closeDialogSearch();
	setDialogAdd();
	closeDialogAdd();
	setDialogUpdate();
	closeDialogUpdate();
	setDialogPda();
	closeDialogPda();
	if(globalMaps.get('2') == undefined) {
		$("#toolbarAdd").hide();
	}
	if(globalMaps.get('4') == undefined) {
		$("#toolbarDelete").hide();
	}
});
function ajaxTable() {
	$('#cmsmdTable').datagrid({
		toolbar : [ {
			id : 'toolbarAdd',
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				openDialogAdd();
			}
		}, '-', {
			id : 'toolbarDelete',
			text : '删除',
			iconCls : 'icon-cancel',
			handler : function() {
				batch('delete');
			}
		}, '-', {
			id : 'toolbarSearch',
			text : '查找',
			iconCls : 'icon-search',
			handler : function() {
				openDialogSearch();
			}
		} , '-', {
			id : 'toolbarSearch',
			text : 'PDA权限',
			iconCls : 'icon-search',
			handler : function() {
				pdaRole();
			}
		} ],
		frozenColumns:[[
			{field:"ck", checkbox:true}
		]],
		pageNumber : 1,
		loadMsg : '数据加载中,请稍后...',
		pageList : [ 10, 30, 50, 100 ],
		onLoadError : function() {
			alert('数据加载失败!');
		},
		queryParams : {
			mdaa : $("#searchMdaa").val(),
			mdac : $("#searchMdac").val(),
			mdad : $("#searchMdad").combobox("getValue")
		},
		onClickRow : function(rowIndex, rowData) {
			$('#cmsmdTable').datagrid('unselectRow', rowIndex);
		},
		onDblClickRow : function(rowIndex, rowData) {
			if(globalMaps.get('3') != undefined) {
				openDialogUpdate(rowData.mdaa,rowData.mdac,rowData.mdad,rowData.mdae,rowData.mdafs);
			}
	    }
	}).datagrid('acceptChanges');
}
function mdadFormatter(value, row, index) {
	if(row.mdad == '') {
		return "";
	}
	return row.mdad+"_"+row.mdadStr;
}

function mdaeFormatter(value, row, index) {
	if(row.mdae == 1) {
		return "男";
	}
	return "女";
}


function reloadTable() {
	$('#cmsmdTable').datagrid('reload');
	$('#cmsmdTable').datagrid('clearSelections');
}
//-------------------------------查询 begin
function setDialogSearch() {
	$('#cmsmdSearch').dialog({
		title : '查询员工',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogSearch() {
	$('#cmsmdSearch').dialog('open');
}
function closeDialogSearch() {
	$('#cmsmdSearch').dialog('close');
}
function searchCmsmd() {
	var queryParams = $('#cmsmdTable').datagrid('options').queryParams;
	queryParams.mdaa = $("#searchMdaa").val();
	queryParams.mdac = $("#searchMdac").val();
	queryParams.mdad = $("#searchMdad").combobox("getValue");
	reloadTable();
	closeDialogSearch();
}
//-------------------------------查询end
//--------------------------添加 Begin
function setDialogAdd() {
	$('#cmsmdAdd').dialog({
		title : '新增员工',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function cmsmdAddReset() {
	$("#cmsmdAddMdaa").val("");
	$("#cmsmdAddMdab").val("");
	$("#cmsmdAddMdac").val("");
	$("#cmsmdAddMdad").combobox("select", "");
	$("#cmsmdAddMdae1").attr("checked", "true");
	$("#cmsmdAddMdaf1").attr("checked", "true");
	$("#cmsmdAddMdaf2").removeAttr("checked");
	$("#cmsmdAddMdaf3").removeAttr("checked");
	$("#cmsmdAddMdaf4").removeAttr("checked");
	$("#cmsmdAddMdaf5").removeAttr("checked");
	$("#cmsmdAddMdaf6").removeAttr("checked");
	$("#cmsmdAddMdaf7").removeAttr("checked");
}
function openDialogAdd() {
	cmsmdAddReset();
	$('#cmsmdAdd').dialog('open');
}
function closeDialogAdd() {
	$('#cmsmdAdd').dialog('close');
}
function cmsmdAdd() {
	var validateResult = $("#addForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#addForm').submit();
}
//--------------------------添加 end
//----------------------------------编辑 BEGIN
function setDialogUpdate() {
	$('#cmsmdUpdate').dialog({
		title : '修改员工',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function closeDialogUpdate() {
	$('#cmsmdUpdate').dialog('close');
}
function openDialogUpdate(mdaa,mdac,mdad,mdae,mdafs) {
	items[0] = mdaa;
	items[1] = mdac;
	items[2] = mdad;
	items[3] = mdae;
	items[4] = mdafs;
	cmsmdUpdateReset(mdaa,mdac,mdad,mdae,mdafs);
	$('#cmsmdUpdate').dialog('open');
}
function cmsmdUpdateReset(mdaa,mdac,mdad,mdae,mdafs) {
	$("#cmsmdInfoUpdateMdaa").val(mdaa);
	$("#cmsmdUpdateMdaa").text(mdaa);
	$("#cmsmdUpdateMdac").val(mdac);
	$("#cmsmdUpdateMdad").combobox("select", mdad);
	if(mdae == 1) {
		$("#cmsmdUpdateMdae1").attr("checked", "true");
	} else {
		$("#cmsmdUpdateMdae2").attr("checked", "true");
	}
	$("#cmsmdUpdateMdaf1").attr("checked", "true");
	$("#cmsmdUpdateMdaf2").removeAttr("checked");
	$("#cmsmdUpdateMdaf3").removeAttr("checked");
	$("#cmsmdUpdateMdaf4").removeAttr("checked");
	$("#cmsmdUpdateMdaf5").removeAttr("checked");
	$("#cmsmdUpdateMdaf6").removeAttr("checked");
	$("#cmsmdUpdateMdaf7").removeAttr("checked");
	$.each(mdafs, function(n, value) {
		if(value == 2) {
			$("#cmsmdUpdateMdaf2").attr("checked", 'true');
		} else if(value == 3) {
			$("#cmsmdUpdateMdaf3").attr("checked", 'true');
		} else if(value == 4) {
			$("#cmsmdUpdateMdaf4").attr("checked", 'true');
		} else if(value == 5) {
			$("#cmsmdUpdateMdaf5").attr("checked", 'true');
		} else if(value == 6) {
			$("#cmsmdUpdateMdaf6").attr("checked", 'true');
		} else if(value == 7) {
			$("#cmsmdUpdateMdaf7").attr("checked", 'true');
		}
	});
}
function cmsmdUpdate() {
	var validateResult = $("#updateForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#updateForm').submit();
}
//-----------------------------------编辑END
//-----------------------------------删除 BEGIN
function batch(flag) {
	if ($('#cmsmdTable').datagrid('getSelected')) {
		var ids = [];
		var selectedRow = $('#cmsmdTable').datagrid('getSelections');
		for ( var i = 0; i < selectedRow.length; i++) {
			ids.push(selectedRow[i].mdaa);	
		}
		var mdaa = ids.join(',');
		if(flag == "delete") {
			$.messager.confirm('删除提示', '你确定永久删除下列员工吗?<br/>' + ids.join(','), function(r) {
				if(r) {
					changeStatus('/wms/cmsmd/deleteCmsmdBatch.xhtml?mdaa=' + mdaa);
				}
			});
		}
		$('#cmsmdTable').datagrid('clearSelections');
	}
}
//-----------------------------------删除END
//-----------------------------------PDA
function setDialogPda() {
	$('#pda').dialog({
		title : 'PDA权限',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogPda() {
	$('#pda').dialog('open');
}
function closeDialogPda() {
	$('#pda').dialog('close');
}
function pdaRole() {
	if ($('#cmsmdTable').datagrid('getSelected')) {
		var selectedRow = $('#cmsmdTable').datagrid('getSelections');
		var mdaa = selectedRow[0].mdaa;
		var mdac = selectedRow[0].mdac;
		cmsmdUpdateReset(mdaa,mdac);
		openDialogPda();
	}
}
function cmsmdUpdateReset(mdaa,mdac) {
	var roleMapArray = new Array();
	var url = "/wms/cmsml/cmsmlList.xhtml?mlab="+mdaa+"&callback=?";
	jQuery.getJSON(url, null, function call(result){   
		jQuery.each(result.data, function(i, obj) {
			var mlac = obj.mlac;
			roleMapArray[i] = mlac;
		});
		$("#pdaHidMdaa").val(mdaa);
		$("#pdaMdaa").text(mdaa);
		$("#pdaMdab").val(mdac);
		$("#checkallupdate").removeAttr("checked");
		$("input[name=daaaUpdate]").removeAttr("checked");
		$("input[name=daaaUpdate]").each(function() {
			var checkBox = $(this);
			$.each(roleMapArray, function(n, value) {
				if(checkBox.val() == value) {
					checkBox.attr("checked", 'true');
				}
			});
		});
	});
}
function pdaUpdate() {
	var validateResult = $("#pdaForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#pdaForm').submit();
}
//-----------------------------------PDA
function changeStatus(url){
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		dataType : "text",
		url : url,
		error : function () {
			alert('请求失败');
		},
		success : function(result){
			if(result=="success") {
				alert("操作成功!");
			} else {
				alert(result);
			}
			reloadTable();
		}
	});
}